<?php

/**
 * indicateur actions.
 *
 * @package    KC-INDICATEURS
 * @subpackage indicateur
 * @author     Antoine Rolland <antoine.rolland@keyconsulting.fr>
 */
class indicateurActions extends sfActions
{

  /**
   * (non-PHPdoc)
   * @see lib/vendor/symfony-1.4.6/lib/action/sfAction::preExecute()
   */
  public function preExecute()
  {
    if(!$this->getUser()->isAuthenticated()
    || !$this->projet = Doctrine::getTable('Projet')->find($this->getUser()->getAttribute('projet_id')))
    {
      $this->getUser()->setAuthenticated(false);
      $this->redirect('transverse/index');
    }
    else{
      $id = $this->getUser()->getAttribute('id');
      $acteur = Doctrine::getTable('Acteur')->findOneByAcronyme($id);
      if ($acteur != null){
        $this->admin = $acteur->isAdmin();
      }
    }
  }

 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->formDr = new IndicateursFormFilter();

    $categorie_id = $request->getParameter('categorie_id',null);

    if($categorie_id === ''){
      $this->response->setContent(Doctrine::getTable('Application')->findAll()->exportTo('json',false));
      return sfView::NONE;
    }

    if($categorie = Doctrine::getTable('Categorie')->find($categorie_id)){
      $this->response->setContent(Doctrine::getTable('Application')->findByCategorieId($categorie->getId())->exportTo('json',false));
      return sfView::NONE;
    }
  }

 /**
  *
  * @param sfRequest $request A request object
  */
  public function executeIndicateurs(sfWebRequest $request)
  {
    $this->redirectUnless($projet = Doctrine::getTable('Projet')->find(
                                    $this->getUser()->getAttribute('projet_id')),
                          '@homepage');

    $granularite = $request->getParameter('granularite',null);

    $categorie_id = $request->getParameter('categorie_id',null);
    $application_id = $request->getParameter('nom_court',null);
    $indicateurs_id = $request->getParameter('indicateur_id',null);
    $periode = $request->getParameter('periode',null);

    $this->debut = null;
    $this->fin = null;

    if($periode){
      if($periode['from']['month'] && $periode['from']['day'] && $periode['from']['year']){
        $this->debut = mktime(0,0,0,$periode['from']['month'],$periode['from']['day'],$periode['from']['year']);
      }
      if($periode['to']['month'] && $periode['to']['day'] && $periode['to']['year']){
        $this->fin = mktime(0,0,0,$periode['to']['month'],$periode['to']['day'],$periode['to']['year']);
      }
    }

    $this->projet = null;
    $this->poles = null;
    $this->applications = null;

    if ($granularite == 0) //Projet TMA DR
    {
      $this->projet = $projet;
      $this->setTemplate('indicateursProjet');
    }

    if ($granularite == 1) //Considérer Pole métier
    {
      if($categorie_id){
        $this->poles = Doctrine::getTable('Categorie')->findById($categorie_id);
      }
      else{
        $this->poles = Doctrine::getTable('Categorie')->findAll();
      }
      $this->setTemplate('indicateursPole');
    }

    if ($granularite == 2) //Considérer par application
    {
      if($application_id){
        //Petite astuce 'findById' pour récupérer directement une collection plutôt qu'un objet
        $this->applications = Doctrine::getTable('Application')->findById($application_id);
      }

      if(!$application_id && $categorie_id){
        $this->applications = Doctrine::getTable('Application')->findByCategorieId($categorie_id);
      }

      if(!$application_id && !$categorie_id){
        $this->applications = Doctrine::getTable('Application')->findAll();
      }
      $this->setTemplate('indicateursApplications');
    }

    if ($indicateurs_id){
      //Petite astuce 'findById' pour récupérer directement une collection plutôt qu'un objet
      $this->indicateurs = Doctrine::getTable('Indicateur')->findById($indicateurs_id);
    }
    else{
      $this->indicateurs = Doctrine::getTable('Indicateur')->findAll();
    }

  }

 /**
  *
  * @param sfRequest $request A request object
  */
  public function executeGraph(sfWebRequest $request)
  {
    $application_id = $request->getParameter('application_id',null);
    $indicateurs_id = $request->getParameter('indicateur_id',null);

//TODO Graph

    return sfView::NONE;
  }

 /**
  *
  * @param sfRequest $request A request object
  */
  public function executeHistorique(sfWebRequest $request)
  {
    $application_id = $request->getParameter('application_id',null);
    $indicateurs_id = $request->getParameter('indicateur_id',null);

//TODO Graph ou historique

    return sfView::NONE;
  }
}
